package com.qys.livesMall.common.utils;

import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

/**
 * @packageName:
 * @className:timestampUtil
 * @Description:
 * @author:guomx
 * @date:2023/7/1217:34
 */
public class TimestampUtil {

    public static Map<String,Long> getCurrentWeekTimeFrame() {
        Map<String,Long> map = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        ZoneId zoneId = ZoneOffset.systemDefault();
        calendar.setTimeZone(TimeZone.getTimeZone(zoneId));
        //start of the week
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            calendar.add(Calendar.DAY_OF_YEAR,-1);
        }
        calendar.add(Calendar.DAY_OF_WEEK, -(calendar.get(Calendar.DAY_OF_WEEK) - 2));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        long startTime = calendar.getTimeInMillis();
        map.put("startTime",startTime);
        //end of the week
        calendar.add(Calendar.DAY_OF_WEEK, 6);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        long endTime = calendar.getTimeInMillis();
        map.put("endTime",endTime);
        return map;
    }
    public static Map<String,Long> getCurrentMonthTimeFrame() {
        Map<String,Long> map = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        ZoneId zoneId = ZoneOffset.systemDefault();
        calendar.setTimeZone(TimeZone.getTimeZone(zoneId));
        //月第一天
        int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);//获取月最大天数
        calendar.set(Calendar.DAY_OF_MONTH, firstDay);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        long firstDayOfMonth  = calendar.getTimeInMillis();
        map.put("firstDayOfMonth ",firstDayOfMonth );
        //月最后一天
        int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);//获取月最大天数
        calendar.set(Calendar.DAY_OF_MONTH, lastDay);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        long lastDayOfMonth  = calendar.getTimeInMillis();
        map.put("lastDayOfMonth ",lastDayOfMonth );
        return map;
    }
}
